package com.crawler.waf.serializer;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

/**
 * 字符串分隔数组
 * @author rubekid
 *
 * 2017年2月21日 下午6:10:26
 */
public class SplitSerializer extends JsonSerializer<String>{
	
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public void serialize(String value, JsonGenerator gen, SerializerProvider provider)
			throws IOException, JsonProcessingException {
		String[] array = new String[0];
		try{
			if(value != null){
				String string  = String.valueOf(value);
				array = string.replaceAll("^,|,$", "").split(",");
			}
		}
		catch(Exception ex){
			logger.error(ex.getMessage(), ex);
		}
		gen.writeObject(array);
	}

}
